﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.APP_UI.Common;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Strategy_Vision : System.Web.UI.Page
    {
        private IStrategyVisionBLL stratCtrl = new StrategyVisionBLL();
        private CommandControl cmdFac = new CommandControl();
        private Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
            }
        }

        private void LoadDataGrid()
        {
            LoadDataGrid(0);
        }

        private void LoadDataGrid(int seq)
        {
            Grid.DataSource = stratCtrl.Search(seq);
            Grid.DataBind();
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            CmdCtrl.Inserted += new EventHandler(OnInsert);
            CmdCtrl.Updated += new EventHandler(OnUpdate);
            CmdCtrl.Deleted += new EventHandler(OnDelete);
        }
        #endregion

        private void OnInsert(object sender, EventArgs e)
        {
            if (stratCtrl.CheckDuplicateCode(txtCode.Text, null))
            {
                main.MessageAlert(Page, "รหัสซ้ำ");
                return;
            }

            if(!main.IsDouble(txtCode.Text)) 
            {
                main.MessageAlert(Page, "กรอกเป็นตัวเลขเท่านั้น");
                return;
            }

            stratCtrl.Insert(txtCode.Text, txtName.Text, txtDescr.Text);
            LoadDataGrid();
            CmdCtrl.RefreshGrid();
        }

        private void OnUpdate(object sender, EventArgs e)
        {
            if (stratCtrl.CheckDuplicateCode(txtCode.Text, hdSeq.Text))
            {
                main.MessageAlert(Page, "รหัสซ้ำ");
                return;
            }

            if(!main.IsDouble(txtCode.Text)) 
            {
                main.MessageAlert(Page, "กรอกเป็นตัวเลขเท่านั้น");
                return;
            }

            stratCtrl.Update(Convert.ToInt32(hdSeq.Text), txtCode.Text, txtName.Text, txtDescr.Text);
            LoadDataGrid();
            CmdCtrl.MapControl(); 
        }

        private void OnDelete(object sender, EventArgs e)
        {
            int nRowSelected = 0;

            if (panelGrid.Visible)
            {
                foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
                {
                    stratCtrl.Delete(Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Sequence"]));

                    nRowSelected++;
                }
            }
            else
            {
                stratCtrl.Delete(Convert.ToInt32(hdSeq.Text));
            }

            LoadDataGrid();
            CmdCtrl.RefreshGrid();
        }

        protected void Grid_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "TargetClick")
            {
                IList<StrategyVisionInfo> stratList = stratCtrl.Search(Convert.ToInt32(((Label)e.Item.FindControl("targetSequence")).Text), "", "", "");

                hdSeq.Text = stratList[0].Sequence.ToString();
                txtCode.Text = stratList[0].Code;
                txtName.Text = stratList[0].Name;
                txtDescr.Text = stratList[0].Description;

                CmdCtrl.MapControl();
            }
            else
            {
                LoadDataGrid();
                CmdCtrl.RefreshGrid();
            }
        }
    }
}
